﻿using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Zeta.Common.Xml;
using Zeta.Game;
using Zeta.XmlEngine;

namespace LocationTracker
{

    [XmlElement("LocationTrackerSettings")]
    class LocationTrackerSettings : XmlSettings
    {
        private static LocationTrackerSettings _instance;
        private bool avoidCombat;
        private float keywardenHpThreshold;
        private static string _battleTagName;

        public static string BattleTagName
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_battleTagName) && ZetaDia.Service.Hero.IsValid)
                    _battleTagName = ZetaDia.Service.Hero.BattleTagName;
                return _battleTagName;
            }
        }

        public LocationTrackerSettings() :
            base(Path.Combine(SettingsDirectory, "LocationTracker", BattleTagName, "Settings.xml"))
        {
        }

        public static LocationTrackerSettings Instance
        {
            get { return _instance ?? (_instance = new LocationTrackerSettings()); }
        }

        [XmlElement("AvoidCombat")]
        [DefaultValue(false)]
        [Setting]
        public bool AvoidCombat
        {
            get
            {
                return avoidCombat;
            }
            set
            {
                avoidCombat = value;
                OnPropertyChanged("AvoidCombat");
            }
        }

        [XmlElement("KeywardenHpThreshold")]
        [DefaultValue(350000f)]
        [Setting]
        public float KeywardenHpThreshold
        {
            get
            {
                return keywardenHpThreshold;
            }
            set
            {
                keywardenHpThreshold = value;
                OnPropertyChanged("KeywardenHpThreshold");
            }
        }
    }

    class LocationTrackerSettingsUI
    {
        public int ServerPort { get; set; }

        private static Window configWindow;

        public static void CloseWindow()
        {
            configWindow.Close();
        }

        public static Window GetDisplayWindow()
        {
            if (configWindow == null)
            {
                configWindow = new Window();
            }

            string assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string xamlPath = Path.Combine(assemblyPath, "Plugins", "LocationTracker", "LocationTracker.xaml");
            string xamlContent = File.ReadAllText(xamlPath);

            UserControl mainControl = (UserControl)XamlReader.Load(new MemoryStream(Encoding.UTF8.GetBytes(xamlContent)));
            configWindow.DataContext = LocationTrackerSettings.Instance;
            configWindow.Content = mainControl;
            configWindow.Width = 450;
            configWindow.Height = 100;
            configWindow.ResizeMode = ResizeMode.NoResize;
            configWindow.Title = "LocationTracker Settings";
            configWindow.Closed += ConfigWindow_Closed;
            Demonbuddy.App.Current.Exit += ConfigWindow_Closed;

            return configWindow;
        }

        static void ConfigWindow_Closed(object sender, System.EventArgs e)
        {
            LocationTrackerSettings.Instance.Save();
            if (configWindow != null)
            {
                configWindow.Closed -= ConfigWindow_Closed;
                configWindow = null;
            }
        }
    }

}
